package com.nand.module.function.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.bm.library.PhotoView;
import com.nand.library.base.base.BaseActivity;
import com.nand.module.function.R;
import com.nand.module.function.adapter.PhotoWallAdapter;
import com.nand.module.function.utils.GlideUtil;
import com.nand.module.function.utils.LogUtils;

import java.util.ArrayList;

/**
 * 图片浏览
 *
 * @author asd
 */
public class ImageActivity extends BaseActivity implements View.OnClickListener {

    public static final String IMG_LIST = "imgList";
    public static final String CURRENT_IMG_URL = "current_img_url";


    public static final String IS_SHOW_TITLE = "IS_SHOW_TITLE";
    private ArrayList<String> imgList;
    private ArrayList<View> mView = new ArrayList<>();
    private TextView index;
    private boolean isShowTitle;
    private int currentIndex = 0;
    private Context mContext;



    @Override
    protected int initLayout() {
        return R.layout.activity_image;
    }

    @Override
    protected void initView() {
        mContext = this;
        initParam();

        initTitle();
        ViewPager vp = findViewById(R.id.photo_wall_vp);
        index = findViewById(R.id.photo_wall_index_tv);
        if (mView != null && mView.size() > 0) {
            PhotoWallAdapter adapter = new PhotoWallAdapter(mView);
            vp.setAdapter(adapter);
            String indexStr = "1 / " + mView.size();
            index.setText(indexStr);
        }

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                String indexStr = position + 1 + " / " + mView.size();
                index.setText(indexStr);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        if (currentIndex > 0) {
            vp.setCurrentItem(currentIndex);
        }
    }

    @Override
    protected void initData() {

    }

    public void initParam() {
        Bundle param = getIntent().getExtras();
        if (param == null) {
            param = new Bundle();
        }
        isShowTitle = param.getBoolean(IS_SHOW_TITLE, false);
        imgList = param.getStringArrayList(IMG_LIST);
        String currentImgUrl = param.getString(CURRENT_IMG_URL, "");

        LogUtils.e("avvv==="+currentImgUrl);
        mView.clear();
        if (imgList != null && imgList.size() > 0) {
            if (imgList.contains(currentImgUrl)) {
                currentIndex = imgList.indexOf(currentImgUrl);
            }
            for (int i = 0; i < imgList.size(); i++) {
                View view = LayoutInflater.from(mContext).inflate(R.layout.photo_wall_item_layout, null);
                PhotoView photoView = view.findViewById(R.id.photo_wall_item_pv);
                photoView.enable();
                photoView.setMaxScale(3);
                GlideUtil.loadImg(mContext, imgList.get(i), photoView);
                mView.add(view);
                photoView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mView.clear();
        mView = null;
        if (imgList != null) {
            imgList.clear();
            imgList = null;
        }
    }



    private void initTitle() {
        ImageView iv = findViewById(R.id.title_back_iv);
        iv.setOnClickListener(this);
        TextView title = findViewById(R.id.title_value_tv);
        title.setText(isShowTitle ? "大图" : "");
        title.setTextColor(Color.BLACK);
        title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

        View titleLayout = findViewById(R.id.title_layout);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ViewGroup.LayoutParams layoutParams = titleLayout.getLayoutParams();
            if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
                // 全屏模式下 避免标题与状态栏重叠, 标题略下移
                ((ViewGroup.MarginLayoutParams) layoutParams).topMargin = 26;
                titleLayout.requestLayout();
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.title_back_iv){
            finish();
        }
    }
}
